var app = angular.module('BaseServer');

app.factory('PublicService', ['$resource', 'PublicFunction', '$injector',
    function($resource, PublicFunction, $injector) {
        //请求返回单个结果
        var request = function(url, isReturnArray, req, successFun, errFun){
            if(successFun == undefined){
                successFun = PublicFunction.successFun;
            }

            if(errFun == undefined){
                errFun = PublicFunction.errFun;
            }

            if(isReturnArray){
                $resource(url, null, { save: { method: 'POST', isArray: true } }).save(req, successFun, errFun);
            }
            else {
                $resource(url).save(req, successFun, errFun);
            }
        };

        return {
            save: function(entityName, records, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Records: records
                    }
                };

                request('/api/entity/save', false, req, successFun, errFun);
            },
            saves: function(entityRecords, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityRecords: entityRecords
                    }
                };

                request('/api/entity/saves', false, req, successFun, errFun);
            },

            updateByValue: function(entityName, condition, value, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition,
                        Value: value
                    }
                };

                request('/api/entity/updateByValue', false, req, successFun, errFun);
            },

            delete: function(entityName, condition, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition
                    }
                };

                request('/api/entity/delete', false, req, successFun, errFun);
            },
            deletes: function(entityName, conditions, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Conditions: conditions
                    }
                };

                request('/api/entity/deletes', false, req, successFun, errFun);
            },
            deleteById: function(entityName, id, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        ID: id
                    }
                };

                request('/api/entity/deleteById', false, req, successFun, errFun);
            },
            deleteByIds: function(entityName, ids, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        IDs: ids
                    }
                };

                request('/api/entity/deleteByIds', false, req, successFun, errFun);
            },

            find: function(entityName, condition, orderBy, populate, successFun, errFun) {
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition,
                        OrderBy: orderBy,
                        Populate: populate
                    }
                };

                request('/api/entity/find', false, req, successFun, errFun);
            },
            findById: function(entityName, id, populate, successFun, errFun) {
                if(populate == null){
                    populate = '';
                }

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        ID: id,
                        Populate: populate
                    }
                };

                request('/api/entity/findById', false, req, successFun, errFun);
            },
            findOne: function(entityName, condition, populate, successFun, errFun) {
                if(populate == null){
                    populate = '';
                }

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition,
                        Populate: populate
                    }
                };

                request('/api/entity/findOne', false, req, successFun, errFun);
            },
            search: function(entityName, text, condition, limit, successFun, errFun) {
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Text: text,
                        Condition: condition,
                        Limit: limit
                    }
                };

                request('/api/entity/search', false, req, successFun, errFun);
            },

            //20160225模糊查询
            queryByKeyword:function(entityName, condition, orderBy, populate, successFun, errFun){
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition,
                        OrderBy: orderBy,
                        Populate: populate
                    }
                };

                request('/api/entity/queryByKeyword', false, req, successFun, errFun);
            },

            pageQuery: function(entityName, condition, orderBy, populate, pageNum, pageSize, successFun, errFun){
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    Condition: condition,
                    OrderBy: orderBy,
                    Populate: populate,
                    PageNum : pageNum,
                    PageSize: pageSize
                };

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        QueryParameter: parameter
                    }
                };

                request('/api/entity/pageQuery', false, req, successFun, errFun);
            },
            queryRecordCount: function(entityName, condition, successFun, errFun){
                var req = {
                    Parameter : {
                        EntityName: entityName,
                        Condition: condition
                    }
                };

                request('/api/entity/queryRecordCount', false, req, successFun, errFun);
            },
            findRecordIndex: function(entityName, id, condition, orderBy, successFun, errFun) {
                if(orderBy == null){
                    orderBy = {};
                }

                var req = {
                    Parameter : {
                        EntityName: entityName,
                        ID: id,
                        Condition: condition,
                        OrderBy: orderBy
                    }
                };

                request('/api/entity/findRecordIndex', false, req, successFun, errFun);
            },

            //获取可扩展枚举
            getExtendEnum: function(enumCode, returnFun){
                var req = {
                    Parameter : {
                        Code: enumCode
                    }
                };

                request('/api/public/getExtendEnum', false, req, function(res){
                        returnFun(res.Data);
                    }
                );
            },
            //获取枚举--通用，普通枚举与扩展枚举的判断用是否包含"."
            getEnum: function(enumCode, returnFun){
                if(enumCode.indexOf (".") > -1){
                    var ary = enumCode.split(".");
                    returnFun($injector.get(ary[0])[ary[1]]);
                }
                else{
                    this.getExtendEnum(enumCode, returnFun);
                }
            },
            //获取系统参数设置
            getSysParameterValue: function(parameterCode, returnFun){
                var req = {
                    Parameter: {
                        Code: parameterCode
                    }
                };

                request('/api/public/getSysParameterValue', false, req, function(res){
                        returnFun(res.Data);
                    }
                );
            }
        };
    }
]);

